package com.jianglin.mapper;

import com.jianglin.model.Parking;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
@Mapper
@Component("parkingDao")
public interface ParkingMapper{
     // 分页查询车位信息（包含Owner信息）
     @Select("<script>" +
             "SELECT park.*, own.username, own.tel " +
             "FROM parking park " +
             "LEFT JOIN owner own ON own.id = park.owner_id " +
             "<where>" +
             "   <if test='numbers != null and numbers != \"\"'>" +
             "       AND park.numbers LIKE CONCAT('%', #{numbers}, '%')" +
             "   </if>" +
             "</where>" +
             "</script>")
     @Results(id = "parkingWithOwner", value = {
             @Result(property = "id", column = "id"),
             @Result(property = "numbers", column = "numbers"),
             @Result(property = "status", column = "status"),
             @Result(property = "ownerId", column = "owner_id"),
             @Result(property = "remarks", column = "remarks"),
             @Result(property = "owner.username", column = "username"),
             @Result(property = "owner.tel", column = "tel")
     })
     List<Parking> queryParkAll(@Param("numbers") String numbers);

     // 查询已使用的车位
     @Select("SELECT * FROM parking WHERE status = 1 AND owner_id IS NOT NULL")
     List<Parking> queryParkAllByStatus();

     // 以下为MyBatis CRUD方法（原BaseMapper中的功能）
     @Select("SELECT * FROM parking WHERE id = #{id}")
     Parking findById(Long id);

     @Insert("INSERT INTO parking (numbers, status, owner_id, remarks) " +
             "VALUES (#{numbers}, #{status}, #{ownerId}, #{remarks})")
     int insert(Parking parking);

     @Update("UPDATE parking SET numbers=#{numbers}, status=#{status}, " +
             "owner_id=#{ownerId}, remarks=#{remarks} WHERE id=#{id}")
     int update(Parking parking);

     @Delete("DELETE FROM parking WHERE id = #{id}")
     int delete(Long id);

     // 分页查询（需配合PageHelper）
     @Select("SELECT * FROM parking")
     List<Parking> findAll();


}
